package com.dragee.modules.work.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.sys.entity.UserEntity;
import com.dragee.modules.sys.service.UserService;
import com.dragee.modules.work.dao.TrainDao;
import com.dragee.modules.work.entity.TrainEntity;
import com.dragee.modules.work.entity.TrainProjectEntity;
import com.dragee.modules.work.service.TrainProjectService;
import com.dragee.modules.work.service.TrainService;
import com.dragee.utils.LoginUserUtil;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("trainService")
public class TrainServiceImpl extends ServiceImpl<TrainDao, TrainEntity> implements TrainService {


    @Resource
    private TrainDao trainDao;
    @Autowired
    private TrainProjectService trainProjectService;

    @Autowired
    private UserService userService;

    private String business = "train";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<TrainEntity> wrapper =
                WrapperUtil.getWrapper(business, params, TrainEntity.class);
        setWrapper(wrapper);
        IPage<TrainEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }

    private QueryWrapper<TrainEntity> setWrapper
            (QueryWrapper<TrainEntity> wrapper ) {
        UserEntity user = userService.getById(LoginUserUtil.getLoginUserId());
        if (StrUtil.isNotBlank(user.getProvince())) {
            wrapper.eq("province", user.getProvince());
        }
        return wrapper;
    }

    @Override
    public TrainEntity getInfoById(String trainId) {
        TrainEntity trainEntity = getById(trainId);
        List<TrainProjectEntity> projects = getProjectByTrain (trainId);
        trainEntity.setProjects(projects);
        return trainEntity;
    }

    @Override
    public List<TrainProjectEntity> getProjectByTrain(String trainId) {
        List<TrainProjectEntity> projects
                = trainProjectService.list(new QueryWrapper<TrainProjectEntity>()
                .eq("trainId", trainId)
                .orderByDesc("sort"));
        return projects;
    }


}